﻿using System;
using System.Collections.Generic;
using System.Data;
using System.DirectoryServices.ActiveDirectory;
using System.Windows.Forms;

namespace HospitalMedicineTraceability
{
    public partial class frmGoodsSetup : DevComponents.DotNetBar.OfficeForm
    {
        public string m_strCode = ""; // 商品编号
        public Dictionary<string, string> m_dictInfo = new Dictionary<string, string>(); // 回传信息(指定对码时用)
        public frmGoodsSetup()
        {
            InitializeComponent();
            cbbIsSupervisionCode.DrawItem += new System.Windows.Forms.DrawItemEventHandler(CommFun._cbbDrawItem);
            cbbIsSplitPackage.DrawItem += new System.Windows.Forms.DrawItemEventHandler(CommFun._cbbDrawItem);
        }
        #region  1.自定义方法
        /// <summary>
        /// 初始化
        /// </summary>
        private void _init() {
            if (!m_strCode.Equals("")) {
                // 重取数据
                string strSql = "select * from TH_Goods where Code = '{0}'";
                strSql = string.Format(strSql, m_strCode);
                DataTable dt = AppInfo._openSql_DataTable(strSql);
                if (dt != null && dt.Rows.Count > 0)
                {
                    m_dictInfo.Add("Id", dt.Rows[0]["Id"].ToString());
                    m_dictInfo.Add("Code", dt.Rows[0]["Code"].ToString());
                    m_dictInfo.Add("Name", dt.Rows[0]["Name"].ToString());
                    m_dictInfo.Add("PackageSpec", dt.Rows[0]["PackageSpec"].ToString());
                    m_dictInfo.Add("Manufacturer", dt.Rows[0]["Manufacturer"].ToString());
                    m_dictInfo.Add("Unit", dt.Rows[0]["Unit"].ToString());
                    m_dictInfo.Add("IsSplitPackage", dt.Rows[0]["IsSplitPackage"].ToString());
                    m_dictInfo.Add("IsSupervisionCode", dt.Rows[0]["IsSupervisionCode"].ToString());
                    m_dictInfo.Add("SplitPackageAmount", dt.Rows[0]["SplitPackageAmount"].ToString());
                }
            }
            if (m_dictInfo.ContainsKey("Code")) {
                // 控件赋初始值
                txtCode0.Text = m_dictInfo["Code"];
                txtName0.Text = m_dictInfo["Name"];
                txtPackageSpec0.Text = m_dictInfo["PackageSpec"];
                txtManufacturer0.Text = m_dictInfo["Manufacturer"];
                txtUnit0.Text = m_dictInfo["Unit"];
                cbbIsSupervisionCode.Text = m_dictInfo["IsSupervisionCode"];
                cbbIsSplitPackage.Text = m_dictInfo["IsSplitPackage"];
                txtSplitPackageAmount.Value = CommDll.FunctionComm._strToInt(m_dictInfo["SplitPackageAmount"]);
            }
        }
        private void _save() {
            
            if (cbbIsSupervisionCode.Text.Equals(""))
            {
                CommFun._messageBoxShow("请先确定是否电监码！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (cbbIsSplitPackage.Text.Equals(""))
            {
                CommFun._messageBoxShow("请先确定是否拆零！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (cbbIsSplitPackage.Text.Equals("是") && txtSplitPackageAmount.Value < 1)
            {
                CommFun._messageBoxShow("拆零数据不能为0！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            
            // 清除原有数据
            string strSql = "Update TH_Goods set IsSupervisionCode = '{1}',IsSplitPackage = '{2}',SplitPackageAmount = '{3}' where Id ={0}";
            strSql = string.Format(strSql, m_dictInfo["Id"], cbbIsSupervisionCode.Text.Trim(), cbbIsSplitPackage.Text.Trim(), txtSplitPackageAmount.Text.Trim());
            m_dictInfo["IsSupervisionCode"] = cbbIsSupervisionCode.Text.Trim();
            m_dictInfo["IsSplitPackage"] = cbbIsSplitPackage.Text.Trim();
            m_dictInfo["SplitPackageAmount"] = txtSplitPackageAmount.Text.Trim();
            AppInfo._execSql(strSql);
            CommFun._messageBoxShow("商品设置成功！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            DialogResult = DialogResult.OK;
        }
        #endregion

        #region 2.系统事件方法
        private void frmEnterpriseCodeCheck_Load(object sender, EventArgs e)
        {
            _init();
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            _save();
        }

        private void btnReturn_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        private void cbbIsSupervisionCode_TextChanged(object sender, EventArgs e)
        {
            if (cbbIsSupervisionCode.Text.Equals("否"))
            {
                cbbIsSplitPackage.Text = "否";
                cbbIsSplitPackage.Enabled = false;
                txtSplitPackageAmount.Value = 0;
                txtSplitPackageAmount.Enabled = false;
            }
            else
            {
                cbbIsSplitPackage.Enabled = true;
                cbbIsSplitPackage.Text = cbbIsSplitPackage.SelectedIndex < 0 ? "否" : cbbIsSplitPackage.Text;
            }
        }

        private void cbbIsSplitPackage_TextChanged(object sender, EventArgs e)
        {
            if (cbbIsSplitPackage.Text.Equals("否"))
            {
                txtSplitPackageAmount.Value = 0;
                txtSplitPackageAmount.Enabled = false;
            }
            else
            {
                txtSplitPackageAmount.Enabled = true;
                txtSplitPackageAmount.Value = 1;
            }
        }
    }
}
